package com.cls.business.strategy.impl.notice;

import com.cls.business.entity.Notice;
import com.cls.business.entity.Standard;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.INoticeService;
import com.cls.business.service.IStandardService;
import com.cls.business.service.ITopicPlanService;
import com.cls.business.strategy.NoticeStrategy;
import com.cls.common.enums.DictEnum;
import com.cls.common.exception.MyException;
import com.cls.common.utils.StringUtils;
import com.cls.system.entity.Configure;
import com.cls.system.service.IConfigureService;
import com.sun.javafx.binding.StringFormatter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * 结项的实现方法
 *
 * @author Administrator
 */
@Component("notice_type_jxgg")
@RequiredArgsConstructor

public class JXGGNoticeImpl implements NoticeStrategy {

    private final INoticeService noticeService;
    private final ITopicPlanService topicPlanService;
    private final IStandardService standardService;
    private final IConfigureService configureService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createNotice(Notice notice) {
        String period = notice.getYear() + "-" + notice.getPlanNo();
        TopicPlan checkPlan = topicPlanService.selectByPeriod(period);
        if(checkPlan!=null){
            throw new MyException("对不起，当前期次已存在！");
        }

        noticeService.save(notice);
        TopicPlan topicPlan = new TopicPlan();
        topicPlan.setConcludeNoticeId(notice.getNoticeId());
        topicPlan.setSelectBeginTime(notice.getSelectBeginTime());
        topicPlan.setYear(notice.getYear());
        topicPlan.setPeriod(period);
        topicPlan.setSelectEndTime(notice.getSelectEndTime());
        topicPlan.setStatusCode(DictEnum.PLAN_CREATE.getCode());
        //设置计划为委托课题
        topicPlan.setTopicClassifyCode(DictEnum.ENTRUST.getCode());
        Standard standard = new Standard();
        standard.setSnapshot(false);
        Long standardId = standardService.snapshotStandard(standard);
        if (standardId < 0) {
            throw new MyException("生成评审标准异常！");
        }
        topicPlan.setStandardId(standardId);

        // 设置全局配置
        Configure configure = new Configure();
        configure.setSnapshot(false);
        Long configureId = configureService.snapshotConfigure(configure);
        if (configureId < 0) {
            throw new MyException("生成全局配置异常！");
        }
        topicPlan.setConfigureId(configureId);

        topicPlanService.save(topicPlan);
    }
}
